package com.borzaindustries.mkdummy;

import java.util.regex.*;

public class SizeFormatter {
	public static String formatSizeString(int size) {
		if (size > 1000000)
			return (size / 1000000) + "M";
		if (size > 1000)
			return (size / 1000) + "K";
		return Integer.toString(size);
	}

	public static int decodeSizeString(String size) throws Exception {
		String temp = size.replaceAll(" ", "").toLowerCase();
		Pattern k = Pattern.compile(".*?([0-9]+).*?");
		Matcher m = k.matcher(temp);
		if (!m.matches())
			throw new NumberFormatException();
		try {
			int value = Integer.parseInt(m.group(1));
			if (temp.contains("m")) // mb,m,mib,mo
				return value * 1000000;

			if (temp.contains("k"))
				return value * 1000; // kb,kib,k,ko
			return value;
		} catch (Exception e) {
			throw new NumberFormatException();
		}
	}
}